<template>
  <page-header-wrapper>
    <a-card :bordered="false" class="user-list">
      <h3 class="title">用户列表 <span>发放过证件的所有人员</span></h3>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="搜索">
                <a-input v-model="queryParam.company_user_keyword" placeholder="输入手机号、姓名等" />
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24">
              <a-form-item label="创建时间">
                <a-range-picker :placeholder="['开始时间', '结束时间']" v-model="date1" />
                <!-- <a-date-picker v-model="queryParam.date1" placeholder="请选择日期" /> -->
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <div class="table-operator-left">
          <!-- <span>操作</span>
          <a-button type="primary" :disabled="!hasSelected" @click="start"> 签发 </a-button>
          <a-button type="primary" :disabled="!hasSelected" @click="start"> 拒签 </a-button> -->
        </div>
        <!-- <a-button type="primary" @click="newAddCard">新增证件模版</a-button> -->
        <span>总计：{{ total }}人</span>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <!-- <a @click="handleDetails(record)">详情</a> -->
            <a @click="handleRemove(record)">删除</a>
            <a-divider type="vertical" />
          </template>
        </span>
        <template slot="avatar" slot-scope="text, record">
          <img :src="record.avatar" alt="" style="width=50px;height:50px;" />
        </template>
      </s-table>
    </a-card>
    <!-- 删除弹窗 -->
    <a-modal title="确认提示" v-model="userVisible" @ok="handleOk" okText="确定" cancelText="取消">
      <p>确认删除？关闭后已发放给该用户的所有证件不可使用，且不可恢复</p>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getUserList, delUser } from '@/api/user'
import moment from 'moment'
import { validatePhone, getProtocol } from '@/utils/publicFun'

const columns = [
  {
    title: '序号',
    dataIndex: 'key',
  },
  {
    title: '证件照',
    dataIndex: 'avatar',
    scopedSlots: { customRender: 'avatar' },
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    scopedSlots: { customRender: 'phone' },
  },
  {
    title: '姓名',
    dataIndex: 'nick_name',
    scopedSlots: { customRender: 'nick_name' },
  },
  {
    title: '创建时间',
    dataIndex: 'create_time',
    scopedSlots: { customRender: 'create_time' },
  },
  {
    title: '首次实名时间',
    dataIndex: 'first_auth_time',
    scopedSlots: { customRender: 'first_auth_time' },
  },
  {
    title: '证件数',
    dataIndex: 'work_card_num',
    scopedSlots: { customRender: 'work_card_num' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '130px',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  components: {
    STable,
    Ellipsis,
  },
  data() {
    this.columns = columns

    return {
      advanced: false,
      date1: [],
      queryParam: {},
      total: '0',
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.queryParam.create_time_start =
          this.date1.length > 0 ? moment(this.date1[0]._d).format('YYYY-MM-DD') + ' 23:59:59' : ''
        this.queryParam.create_time_end =
          this.date1.length > 0 ? moment(this.date1[1]._d).format('YYYY-MM-DD') + ' 23:59:59' : ''
        const requestParameters = Object.assign({}, parameter, this.queryParam)

        console.log(requestParameters)
        return getUserList(requestParameters)
          .then((res) => {
            console.log(res)

            if (res.data.list) {
              this.total = res.data.list.length
              res.data.list.forEach((v, i) => {
                v.key = i + 1
                v.avatar = getProtocol() + v.avatar
              })

              return res.data
            } else {
              return {
                count: 0,
                list: [],
              }
            }
          })
          .catch((err) => {
            console.log(err)
          })
      },
      userVisible: false,
      whoUser: {},
    }
  },
  methods: {
    // 重置按钮

    handleReset() {
      this.queryParam = {}
      this.date1 = []
      this.$refs.table.refresh(true)
    },
    // 删除
    handleRemove(record) {
      this.whoUser = record
      this.userVisible = true
    },
    // 确认按钮
    handleOk() {
      delUser({ uid: this.whoUser.user_id })
        .then((res) => {
          this.userVisible = false
          this.$message.success(res.msg)
          this.$refs.table.refresh(true)
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
}
</script>

<style lang="less" scoped>
.user-list {
  background: #fff;
  .title {
    padding-bottom: 10px;
    margin-bottom: 20px;
    border-bottom: 1px solid #eee;
    font-weight: bold;
    span {
      font-size: 12px;
      margin-left: 10px;
      color: #999;
    }
  }
}
</style>